package br.puc.rio.alessandra;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.crypto.spec.DESedeKeySpec;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import br.puc.rio.alessandra.CarregamentoRedeExtension;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JComboBox;

import com.vividsolutions.jump.feature.AttributeType;
import com.vividsolutions.jump.feature.FeatureSchema;
import com.vividsolutions.jump.workbench.model.Layer;

public class TelaCluster extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7873224945328218193L;
	ClusterPlugIn clusterPlugIn;
	 JComboBox comboBox_selecioneID ;
	 JComboBox comboBox_selecioneCamada;
	
	public TelaCluster(ClusterPlugIn aPlugIn) {
		
		super(aPlugIn.getPlugInContext().getWorkbenchFrame(),"Importar Arquivo", false);
		setTitle("Procedimento KRUSKAL");
		this.clusterPlugIn = aPlugIn;
		
		setBounds(100, 100, 560, 236);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 28, 52, 44, 76, 57, 0, 0, 29, 0};
		gridBagLayout.rowHeights = new int[]{23, 26, 0, 0, 0, 115, 0, 0, 30, 0, 27, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblDefina = new JLabel("1 - Selecione a camada");
		lblDefina.setForeground(Color.BLACK);
		lblDefina.setBackground(Color.ORANGE);
		GridBagConstraints gbc_lblDefina = new GridBagConstraints();
		gbc_lblDefina.anchor = GridBagConstraints.WEST;
		gbc_lblDefina.gridwidth = 3;
		gbc_lblDefina.insets = new Insets(0, 0, 5, 5);
		gbc_lblDefina.gridx = 1;
		gbc_lblDefina.gridy = 4;
		getContentPane().add(lblDefina, gbc_lblDefina);
		
		final JComboBox comboBox_selecioneCamada = new JComboBox<>(clusterPlugIn.getLayers());
		
		GridBagConstraints gbc_comboBox_selecioneCamada = new GridBagConstraints();
		gbc_comboBox_selecioneCamada.gridwidth = 2;
		gbc_comboBox_selecioneCamada.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_selecioneCamada.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_selecioneCamada.gridx = 4;
		gbc_comboBox_selecioneCamada.gridy = 4;
		getContentPane().add(comboBox_selecioneCamada, gbc_comboBox_selecioneCamada);
		
		JLabel lblSelecione = new JLabel("2 - Selecione o ID*");
		lblSelecione.setForeground(Color.BLACK);
		lblSelecione.setBackground(Color.ORANGE);
		GridBagConstraints gbc_lblSelecione = new GridBagConstraints();
		gbc_lblSelecione.anchor = GridBagConstraints.WEST;
		gbc_lblSelecione.gridwidth = 2;
		gbc_lblSelecione.insets = new Insets(0, 0, 5, 5);
		gbc_lblSelecione.gridx = 1;
		gbc_lblSelecione.gridy = 6;
		getContentPane().add(lblSelecione, gbc_lblSelecione);
		
		comboBox_selecioneID = new JComboBox();
		setComboAttributes(comboBox_selecioneID, comboBox_selecioneCamada.getSelectedItem());
		
comboBox_selecioneCamada.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setComboAttributes(comboBox_selecioneID, comboBox_selecioneCamada.getSelectedItem());
				
			}
		});
		
		GridBagConstraints gbc_comboBox_selecioneID = new GridBagConstraints();
		gbc_comboBox_selecioneID.gridwidth = 2;
		gbc_comboBox_selecioneID.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_selecioneID.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_selecioneID.gridx = 4;
		gbc_comboBox_selecioneID.gridy = 6;
		getContentPane().add(comboBox_selecioneID, gbc_comboBox_selecioneID);
		
		JLabel lblExecute = new JLabel("3 - Execute");
		lblExecute.setForeground(Color.BLACK);
		lblExecute.setBackground(Color.ORANGE);
		GridBagConstraints gbc_lblExecute = new GridBagConstraints();
		gbc_lblExecute.insets = new Insets(0, 0, 5, 5);
		gbc_lblExecute.gridx = 1;
		gbc_lblExecute.gridy = 7;
		getContentPane().add(lblExecute, gbc_lblExecute);
		
		JButton btn_gravarPontos = new JButton("Gravar");
		GridBagConstraints gbc_btn_gravarPontos = new GridBagConstraints();
		gbc_btn_gravarPontos.fill = GridBagConstraints.HORIZONTAL;
		gbc_btn_gravarPontos.gridwidth = 2;
		gbc_btn_gravarPontos.insets = new Insets(0, 0, 5, 5);
		gbc_btn_gravarPontos.gridx = 4;
		gbc_btn_gravarPontos.gridy = 7;
		getContentPane().add(btn_gravarPontos, gbc_btn_gravarPontos);
		
		JLabel lblDeveseEscolher = new JLabel("* Deve-se escolher um campo que possua ID exclusivo. Ex: COD_IBGE");
		GridBagConstraints gbc_lblDeveseEscolher = new GridBagConstraints();
		gbc_lblDeveseEscolher.gridwidth = 8;
		gbc_lblDeveseEscolher.insets = new Insets(0, 0, 5, 5);
		gbc_lblDeveseEscolher.gridx = 1;
		gbc_lblDeveseEscolher.gridy = 9;
		getContentPane().add(lblDeveseEscolher, gbc_lblDeveseEscolher);
		
		btn_gravarPontos.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clusterPlugIn.setMatrizDistancias(
						comboBox_selecioneCamada.getSelectedItem().toString(),
						comboBox_selecioneID.getSelectedItem().toString()
						);
				clusterPlugIn.geraLayerArestasResult();
			}
		});
		
		
	}
	private void setComboAttributes(JComboBox combo, Object aSelectedLayer) {
    	if (aSelectedLayer != null) {
        	FeatureSchema schema = ((Layer)aSelectedLayer).getFeatureCollectionWrapper().getFeatureSchema();
            
        	((DefaultComboBoxModel)combo.getModel()).removeAllElements();
                    	
            for (int i = 0 ; i < schema.getAttributeCount() ; i++) {
                if (schema.getAttributeType(i) == AttributeType.INTEGER ||
                	schema.getAttributeType(i) == AttributeType.STRING  || 
                	schema.getAttributeType(i) == AttributeType.DOUBLE  || 
                	schema.getAttributeType(i) == AttributeType.OBJECT)  {
                    ((DefaultComboBoxModel)combo.getModel()).addElement(schema.getAttributeName(i));
                }
            }
    	}
    }
}
